package de.hs_zigr.inf.iib08gruppe5powerfish.it.control;

import java.io.IOException;

import org.xmldb.api.base.XMLDBException;

import de.hs_zigr.inf.iib08gruppe5powerfish.it.entity.Connection;

/**
 * Control Class for Driver and Collection of eXist db
 */
public class CtrlDBDriver_Collection {

	/**
	 * sets current driver and collection of data base
	 * 
	 * @throws XMLDBException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws NullPointerException
	 * @throws IOException
	 */
	public CtrlEXistDriver setDBConnetion() throws NullPointerException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException, XMLDBException, IOException {
		
		/** driver string of eXist data base */
		String strDriver = "";
		/** collection string of eXist data base */
		String strCollection = "";
		CtrlEXistDriver eXistDriver = null;
		// load stored settings
		Connection conLoader = new Connection();
		strCollection = conLoader.getCollection();
		strDriver = conLoader.getDriver();
		// store default collection and driver string if one of them is
		// unspecified
		if (strCollection.equals("") || strDriver.equals("")) {
			strCollection = "xmldb:exist://localhost:8080/exist/xmlrpc/db/test";
			strDriver = "org.exist.xmldb.DatabaseImpl";
			Connection connection = new Connection();
			connection.setDriver(strDriver);
			connection.setCollection(strCollection);
		}
		eXistDriver = new CtrlEXistDriver();
		eXistDriver.setUpDBConnection(strDriver, strCollection);
		return eXistDriver;
	}
}